package com.stephenone.servicedemo;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.stephenone.handlerdemo.R;

public class Main2Activity extends AppCompatActivity {

    private TextView tv_num;
    private MyService.MyBinder binder;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    } ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_2);
        tv_num = (TextView) findViewById(R.id.tv_num);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    mHandler.sendEmptyMessage(66);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    public ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder = (MyService.MyBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    public void startClick(View v){
//        ValueAnimator valueAnimator =  ValueAnimator.ofInt(0,100);
//        valueAnimator.setDuration(3000);
//        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator valueAnimator) {
//                tv_num.setText((Integer) valueAnimator.getAnimatedValue() + "");
//            }
//        });
//        valueAnimator.setInterpolator(new DecelerateInterpolator());
//        valueAnimator.start();

        //Scheme跳转协议
        String textMessage = "66";

        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
        sendIntent.setType("text/plain");


// Verify that the intent will resolve to an activity
        if (sendIntent.resolveActivity(getPackageManager()) != null) {
            startActivity(sendIntent);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
    }

    public void onBindClick(View v){
        bindService(new Intent(this, MyService.class),conn , Service.BIND_AUTO_CREATE);
    }

    public void onUnbindClick(View v){
        unbindService(conn);
    }

    public void onGetStatusClick(View v){
        Toast.makeText(this,"count:" + binder.getCount(), Toast.LENGTH_SHORT).show();
    }

    public void onIntentServiceClick(View view){
        startService(new Intent(this,HelloIntentService.class));
    }


//一般按生命周期方法绑定和解绑Service
//    @Override
//    protected void onStart() {
//        super.onStart();
//        // Bind to LocalService
//        Intent intent = new Intent(this, LocalService.class);
//        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
//    }
//
//    @Override
//    protected void onStop() {
//        super.onStop();
//        // Unbind from the service
//        if (mBound) {
//            unbindService(mConnection);
//            mBound = false;
//        }
//    }


}
